package com.ulook.lookplanet.controller;

import com.ulook.lookplanet.bean.VArticlePreInfo;
import com.ulook.lookplanet.common.ExceptionCode;
import com.ulook.lookplanet.common.JsonResult;
import com.ulook.lookplanet.service.TopicService;
import com.ulook.lookplanet.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 描述：
 *
 * @author L1998
 * @date 2021/4/21
 */
@RestController
@RequestMapping("/topic")
public class TopicController {

    @Autowired
    private TopicService service;

    /**
     * 通过小组uid或话题uid获取文章
     */
    @PostMapping("/getArticleList")
    public JsonResult<List<VArticlePreInfo>> getArticleList(HttpServletRequest request){

        String uid = request.getParameter("uid");
        String userId = request.getParameter("currUserId");
        String itemNum = request.getParameter("itemNum");
        String isTopic = request.getParameter("isTopic");

        if(StringUtil.isEmpty(uid) || StringUtil.isEmpty(userId)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数为空");
        }

        List<VArticlePreInfo> articlePreInfos;
        if(isTopic == null){
            articlePreInfos = service.getArticleForComuId(uid, Integer.parseInt(userId), Integer.parseInt(itemNum));
        }else {
            articlePreInfos = service.getArticleForTopicId(uid, Integer.parseInt(userId), Integer.parseInt(itemNum));
        }
        if(articlePreInfos.size() == 0){
            return new JsonResult<>(ExceptionCode.NO_DATA, "没有更多数据了");
        }
        return new JsonResult<>(articlePreInfos);
    }

    /**
     * 根据用户id搜索帖子
     */
    @PostMapping("/getArticleByUserId")
    public JsonResult<List<VArticlePreInfo>> getArticleByUserId(HttpServletRequest request){
        String userId = request.getParameter("currUserId");
        String itemNum = request.getParameter("itemNum");

        if(StringUtil.isEmpty(userId)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数为空");
        }

        List<VArticlePreInfo> articlePreInfos = service.getArticleByUserId(Integer.parseInt(userId), Integer.parseInt(itemNum));
        if(articlePreInfos.size() == 0){
            return new JsonResult<>(ExceptionCode.NO_DATA, "没有更多数据了");
        }
        return new JsonResult<>(articlePreInfos);
    }

    /**
     * 删除
     */
    @PostMapping("/deleteArticleByUid")
    public JsonResult deleteArticleByUid(HttpServletRequest request){
        String uid = request.getParameter("uid");
        if(StringUtil.isEmpty(uid)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数为空");
        }
        service.deleteArticleByUid(uid);
        return new JsonResult();
    }
}
